#include "HufArc.h"

FILE ** openArc (char * arcName, char * arcnew)
{
	int rc = 0;
	char a[] = ".awc";
	char buf[]=_SIGN;
	FILE ** arc = (FILE **) calloc (2, sizeof (FILE *));
	struct stat * arcstat = (struct stat *) calloc (1,sizeof(struct stat));
	if (!arc)
		cl_exit (OPEN_ARC,MEMORY_ERR,arc,arcstat,arcnew);
	if (!arcstat)
		cl_exit (OPEN_ARC,MEMORY_ERR,arc,arcstat,arcnew);
	arcnew = mktemp (arcnew);
	if ((arc[0] = fopen (arcName,"rb")))
	{
		if (!(rc = checkSign(arc[0])))
		{
			stat (arcName,arcstat);
			if (!(arcstat->st_mode & _S_IWRITE))
			{
				cl_exit (OPEN_ARC,ARC_IWRITE,arc,arcstat,arcnew);
				return NULL;
			}
			if ((arc[1] = fopen (arcnew,"wb")))
			{		
				return arc;
			} else {
				cl_exit (OPEN_ARC,OPEN_ERR,arc,arcstat,arcnew);
				return NULL;
			}
		} else {
			cl_exit (OPEN_ARC,SIGN_ERR,arc,arcstat,arcnew);
			return NULL;
		}
	} else {
		arc[0] = NULL;
		if ((arc[1] = fopen (arcnew,"wb")))
		{
			return arc;
		} else {
			cl_exit (OPEN_ARC,OPEN_ERR,arc,arcstat,arcnew);
			return NULL;
		}
	}
return NULL;
}
